package com.hm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.hm.service.ComputeClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

/**
 * @author shishun.wang
 * @date 2016年10月31日 下午10:24:27
 * @version 1.0
 * @describe
 */
@RestController
public class ConsumerController {

	@Autowired
	private RestTemplate restTemplate;

	@Autowired
	private ComputeClient computeClient;

	@HystrixCommand(fallbackMethod="addServiceFallback")
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add() {
		return restTemplate.getForEntity(
				"http://compute-service/add?a=" + ((int) (Math.random() * 100)) + "&b=" + ((int) (Math.random() * 100)),
				String.class).getBody();
	}

	@RequestMapping(value = "/newAdd", method = RequestMethod.GET)
	public Integer newAdd() {
		System.out.println("ComputeClient:/newAdd");
		int tag = (int) (Math.random() * 100);
		return computeClient.add(tag, tag);
	}
	
	public String addServiceFallback() {
        return "远程调用错误";
    }
}
